{% extends "pretixcontrol/event/base.html" %}
{% load i18n %}
{% load eventurl %}
{% load urlreplace %}
{% load money %}
{% load bootstrap3 %}
{% block title %}{% trans "Orders" %}{% endblock %}
{% block content %}
    <nav id="event-nav" class="header-nav">
        <div class="navigation">
            <div class="navigation-title">
                <h1>{% trans "Orders" %}</h1>
            </div>
            {% include "pretixcontrol/event/component_link.html" %}
        </div>
    </nav>
    {% if not filter_form.filtered and orders|length == 0 and not filter_strings %}
        <div class="empty-collection">
            <p>
                {% blocktrans trimmed %}
                    Nobody ordered a ticket yet.
                {% endblocktrans %}
            </p>

            {% if not request.event.live %}
                <a href="{% url "control:event.live" event=request.event.slug organizer=request.event.organizer.slug %}"
                        class="btn btn-primary btn-lg">
                    {% trans "Take your shop live" %}
                </a>
            {% else %}
                <a href="{% eventurl request.event "presale:event.index" %}" class="btn btn-primary btn-lg" target="_blank">
                    {% trans "Go to the ticket shop" %}
                </a>
            {% endif %}
        </div>
    {% else %}
        {% if filter_strings %}
            <p>
                <span class="fa fa-filter"></span>
                {% trans "Search query:" %}
                {{ filter_strings|join:" · " }}
                ·
                <a href="{% url "control:event.orders.search" event=request.event.slug organizer=request.event.organizer.slug %}?{{ request.META.QUERY_STRING }}">
                    <span class="fa fa-edit"></span>
                    {% trans "Edit" %}
                </a>
            </p>
        {% else %}
            <div class="row filter-form">
                <form class="col-md-2 col-xs-12"
                        action="{% url "control:event.orders.go" event=request.event.slug organizer=request.event.organizer.slug %}">
                    <div class="input-group">
                        <input type="text" name="code" class="form-control" placeholder="{% trans "Order code" %}" autofocus>
                        <span class="input-group-btn">
                            <button class="btn btn-primary" type="submit">{% trans "Go!" %}</button>
                        </span>
                    </div>
                </form>
                <form class="" action="" method="get">
                    <div class="col-md-2 col-xs-6">
                        {% bootstrap_field filter_form.status layout='inline' %}
                    </div>
                    {% if request.event.has_subevents %}
                        <div class="col-md-1 col-xs-6">
                            {% bootstrap_field filter_form.product layout='inline' %}
                        </div>
                        <div class="col-md-2 col-xs-6">
                            {% bootstrap_field filter_form.subevent layout='inline' %}
                        </div>
                        <div class="col-md-1 col-xs-6">
                            {% bootstrap_field filter_form.provider layout='inline' %}
                        </div>
                    {% else %}
                        <div class="col-md-2 col-xs-6">
                            {% bootstrap_field filter_form.product layout='inline' %}
                        </div>
                        <div class="col-md-2 col-xs-6">
                            {% bootstrap_field filter_form.provider layout='inline' %}
                        </div>
                    {% endif %}
                    <div class="col-md-2 col-xs-6">
                        {% bootstrap_field filter_form.query layout='inline' %}
                    </div>
                    <div class="col-md-1 col-xs-6">
                        <button class="btn btn-primary btn-block" type="submit">
                            <span class="fa fa-filter"></span>
                        </button>
                    </div>
                    <div class="col-md-1 col-xs-6">
                        <a href="{% url "control:event.orders.search" event=request.event.slug organizer=request.event.organizer.slug %}" class="btn btn-default btn-block" type="submit" data-toggle="tooltip" title="{% trans "Advanced search" %}">
                            <span class="fa fa-cog"></span>
                        </a>
                    </div>
                </form>
            </div>
        {% endif %}
        {% if filter_form.is_valid and filter_form.cleaned_data.question %}
            <p class="text-muted">
                <span class="fa fa-filter"></span>
                {% blocktrans trimmed with question=filter_form.cleaned_data.question.question %}
                    List filtered by answers to question "{{ question }}".
                {% endblocktrans %}
                <a href="?{% url_replace request 'question' '' 'answer' ''%}" class="text-muted">
                    <span class="fa fa-times"></span>
                    {% trans "Remove filter" %}
                </a>
            </p>
        {% endif %}
        <div class="table-responsive">
            <table class="table table-condensed table-hover table-orders">
                <thead>
                <tr>
                    <th>{% trans "Order code" %}
                        <a href="?{% url_replace request 'ordering' '-code' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'code' %}"><i class="fa fa-caret-up"></i></a></th>
                    <th>{% trans "User" %}
                        <a href="?{% url_replace request 'ordering' '-email' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'email' %}"><i class="fa fa-caret-up"></i></a></th>
                    <th>{% trans "Order date" %}
                        <a href="?{% url_replace request 'ordering' '-datetime' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'datetime' %}"><i class="fa fa-caret-up"></i></a>
                    </th>
                    <th class="text-right flip">{% trans "Order paid / total" %}
                        <a href="?{% url_replace request 'ordering' '-total' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'total' %}"><i class="fa fa-caret-up"></i></a></th>
                    <th class="text-right flip">{% trans "Positions" %}</th>
                    <th class="text-right flip">{% trans "Status" %}
                        <a href="?{% url_replace request 'ordering' '-status' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'status' %}"><i class="fa fa-caret-up"></i></a></th>
                </tr>
                </thead>
                <tbody>
                {% for o in orders %}
                    <tr>
                        <td>
                            <strong>
                                <a
                                        href="{% url "control:event.order" event=request.event.slug organizer=request.event.organizer.slug code=o.code %}">
                                    {{ o.code }}</a>
                            </strong>
                            {% if o.testmode %}
                                <span class="label label-warning">{% trans "TEST MODE" %}</span>
                            {% endif %}
                        </td>
                        <td>
                            {{ o.email|default_if_none:"" }}
                            {% if o.invoice_address.name %}
                                <br>{{ o.invoice_address.name }}
                            {% endif %}
                        </td>
                        <td>
                            <span class="fa fa-{{ o.sales_channel_obj.icon }} text-muted"
                                  data-toggle="tooltip" title="{% trans o.sales_channel_obj.verbose_name %}"></span>
                            {{ o.datetime|date:"SHORT_DATETIME_FORMAT" }}
                        </td>
                        <td class="text-right flip">
                            {% if o.has_cancellation_request %}
                                <span class="label label-warning">{% trans "CANCELLATION REQUESTED" %}</span>
                            {% endif %}
                            {% if o.has_external_refund or o.has_pending_refund %}
                                <span class="label label-danger">{% trans "REFUND PENDING" %}</span>
                            {% elif o.has_pending_refund %}
                                <span class="label label-warning">{% trans "REFUND PENDING" %}</span>
                            {% endif %}
                            {% if o.is_overpaid %}
                                <span class="label label-warning">{% trans "OVERPAID" %}</span>
                            {% elif o.is_underpaid  %}
                                <span class="label label-danger">{% trans "UNDERPAID" %}</span>
                            {% elif o.is_pending_with_full_payment %}
                                <span class="label label-danger">{% trans "FULLY PAID" %}</span>
                            {% endif %}
                            {% if o.computed_payment_refund_sum == o.total or o.computed_payment_refund_sum == 0 %}
                                <span class="text-muted">
                            {% endif %}
                            {{ o.computed_payment_refund_sum|money:request.event.currency }} /
                            {% if o.computed_payment_refund_sum == o.total or o.computed_payment_refund_sum == 0 %}
                                </span>
                            {% endif %}
                            {{ o.total|money:request.event.currency }}
                            {% if o.status == "c" and o.icnt %}
                                <br>
                                <span class="label label-warning">{% trans "INVOICE NOT CANCELED" %}</span>
                            {% endif %}
                        </td>
                        <td class="text-right flip">{{ o.pcnt|default_if_none:"0" }}</td>
                        <td class="text-right flip">{% include "pretixcontrol/orders/fragment_order_status.html" with order=o %}</td>
                    </tr>
                {% endfor %}
                </tbody>
                {% if sums %}
                    <tfoot>
                    <tr>
                        <th>{% trans "Sum over all pages" %}</th>
                        <th></th>
                        <th>
                            {% blocktrans trimmed count s=sums.c %}
                                1 order
                            {% plural %}
                                {{ s }} orders
                            {% endblocktrans %}
                        </th>
                        <th class="text-right flip">
                            {% if sums.s|default_if_none:"none" != "none" %}
                                {{ sums.s|money:request.event.currency }}
                            {% endif %}
                        </th>
                        <th class="text-right flip">
                            {% if sums.pc %}
                                {{ sums.pc }}
                            {% endif %}
                        </th>
                        <th></th>
                    </tr>
                    </tfoot>
                {% endif %}
            </table>
        </div>
        {% include "pretixcontrol/pagination.html" %}
    {% endif %}
{% endblock %}
